package com.hghivln.erpwarehouse.system.controller;


import com.hghivln.erpwarehouse.system.common.lang.Result;
import com.hghivln.erpwarehouse.system.entity.Role;
import com.hghivln.erpwarehouse.system.service.RoleService;
import com.hghivln.erpwarehouse.system.vo.RoleVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hghivln
 * @since 2021-07-01
 */
@RestController
@RequestMapping("api/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 查询角色
     */
    @RequestMapping("loadAllRole")
    public Object loadAllRole(RoleVo roleVo) {
        return roleService.queryAllRole(roleVo);
    }

    @RequestMapping("loadAllRoleNoPage")
    public Object loadAllRoleNoPage() {
        return roleService.queryAllRoleNoPage();
    }

    /**
     * 添加角色
     */
    @RequestMapping("addRole")
    @RequiresPermissions("role:add")
    public Result addRole(Role role) {
        try {
            roleService.addRole(role);
            return Result.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ADD_ERROR;
        }
    }

    /**
     * 修改角色
     */
    @RequestMapping("updateRole")
    @RequiresPermissions("role:update")
    public Result updateMenu(Role role) {
        try {
            roleService.updateRole(role);
            return Result.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }

    /**
     * 删除角色
     */
    @RequestMapping("deleteRole")
    @RequiresPermissions("role:delete")
    public Result deleteRole(Integer id) {
        try {
            roleService.removeById(id);
            return Result.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.DELETE_ERROR;
        }
    }

    /**
     * 根据角色ID查询当前角色已拥有的菜单id集合
     */
    @RequestMapping("queryMenuIdsByRid")
    public Object queryMenuIdsByRid(Integer rid){
        return roleService.queryMenuIdsByRid(rid);
    }

    /**
     * 保存角色和菜单之间的关系
     */
    @RequestMapping("saveRoleMenu")
    public Result saveRoleMenu(Integer rid,Integer[] mids){
        try {
            roleService.saveRoleMenu(rid,mids);
            return Result.DISPATCH_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.DISPATCH_ERROR;
        }
    }

}
